﻿$.fn.extend({
    button: function (dom) {
        $(this).each(function (index, item) {
            var btn = $(item);
            var id = btn.attr('ID');
            if (dom == "loading") {
                var loadintext = btn.attr('data-loading-text');
                var txt = btn.html();
                btn.attr('data-htmltextval', txt);
                loadintext = loadintext == undefined || loadintext == "" ? '<i class="layui-icon layui-anim layui-anim-rotate layui-anim-loop">&#xe63d;</i>Loading' : loadintext;
                btn.addClass('layui-btn-disabled').attr('disabled', 'disabled').html(loadintext);
            } else if (dom == 'reset') {
                var txt = btn.attr('data-htmltextval');
                btn.removeClass('layui-btn-disabled').removeAttr('disabled');
                if (txt != undefined && txt != '') {
                    btn.html(txt);
                }
            }
        });
    },
    layvalid: function () {
        //开始校验
        var verify = {
            required: [
              /[\S]+/
              , '必填项不能为空'
            ]
               , phone: [
                 /^1\d{10}$/
                 , '请输入正确的手机号'
               ]
               , email: [
                 /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
                 , '邮箱格式不正确'
               ]
               , url: [
                 /(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/
                 , '链接格式不正确'
               ]
               , number: function (value) {
                   if (!value || isNaN(value)) return '只能填写数字'
               }
               , date: [
                 /^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/
                 , '日期格式不正确'
               ]
               , identity: [
                 /(^\d{15}$)|(^\d{17}(x|X|\d)$)/
                 , '请输入正确的身份证号'
               ]
               , ip: [
                /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
                , '请输入正确的链接'
               ]
               , Price: [
               /^\+?[0-9]\d*$/
                , '请输入正确的价格'
               ]
        };
        var device = layui.device()
        var stop = true;
        var DANGER = 'layui-form-danger';
        var verifyElem = $(this).find("*[lay-verify]");
        for (var i = 0; i < verifyElem.length; i++) {
            var item = verifyElem[i];
            var othis = $(item);
            var vers = othis.attr('lay-verify').split('|');
            var verType = othis.attr('lay-verType'); //提示方式
            var value = othis.val();
            othis.removeClass(DANGER);
            var error = []; //错误提示文本
            layui.each(vers, function (_, thisVer) {
                if (verify[thisVer] != undefined) {
                    if (verify[thisVer][0].test(value) == false) {
                        error.push(verify[thisVer][1]);
                    }
                } else {
                    try {
                        var str = eval(thisVer + "()");
                        if (str != undefined && str.length > 0) {
                            error.push(str);
                        }
                    } catch (e) {
                        error.push(thisVer + ">不是有效的验证函数");
                    }
                }
            });
            if (error.length > 0) {
                var errorText = error.join("<br />")
                //提示层风格
                if (verType === 'tips') {
                    layer.tips(errorText, { tips: 1 });
                } else if (verType === 'alert') {
                    layer.alert(errorText, { title: '提示', shadeClose: true });
                } else {
                    layer.msg(errorText, { icon: 5, shift: 6 });
                }
                if (!device.android && !device.ios) {
                    item.focus();
                } //非移动设备自动定位焦点
                othis.addClass(DANGER);
                return stop = false;
            }
        }
        return stop;
    }

});

/*
numberbox 插件
添加layui-numberbox 即可实现只能输入数字
参数：
    persion=精度，默认值0，输入值需大于等于0
    min=最小值
    max=最大值
重新渲染方法:$.parseNumberbox(),用于动态html
实例：
<input type="text" class="layui-numberbox" data-persion='3' data-min="-0.21" data-max="323.99" />
*/
$.extend({
    parseNumberbox: function () {
        $(".layui-numberbox").each(function (index, item) {
            var option = {
                persion: 0,
                min: null,
                max: null,
            }
            var persion = $(item).attr('data-persion');
            if (persion != undefined && isNaN(persion) == false) {
                option.persion = parseInt(persion);
            }
            var min = $(item).attr('data-min');
            if (min != undefined && isNaN(min) == false) {
                option.min = parseFloat(min);
            }
            var max = $(item).attr('data-max');
            if (max != undefined && isNaN(max) == false) {
                option.max = parseFloat(max);
            }
            $(item).keyup(function (e) {
                var val = $(e.target).val();
                if (val.length > 0) {
                    //得到第一个字符是否为负号  
                    var t = val.charAt(0);
                    //先把非数字的都替换掉，除了数字和.   

                    if (option.persion <= 0) {
                        val = val.replace(/[^\d]/g, '');
                    } else {
                        //先把非数字的都替换掉，除了数字和.   
                        val = val.replace(/[^\d\.]/g, '');
                        //必须保证第一个为数字而不是.     
                        val = val.replace(/^\./g, '');
                        //保证只有出现一个.而没有多个.     
                        val = val.replace(/\.{,}/g, '');
                        val = val.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.');
                    }
                    if (t == "-") {
                        val = '-' + val;
                    }
                    if (option.min >= 0 && val.substr(0, 1) == '-') {
                        val = val.replace('-', '');
                    }
                    if (val.length > 0) {
                        var num = option.persion <= 0 ? parseInt(val) : parseFloat(val).toFixed(option.persion);

                        if (option.min != null && option.min > num) {
                            num = option.min.toFixed(option.persion);
                        }
                        if (option.max != null && option.max < num) {
                            num = option.max.toFixed(option.persion);
                        }
                        $(e.target).val(num);
                    } else {
                        $(e.target).val('');
                    }
                }
            });
        });
    }
});
$(function () {
    $.parseNumberbox();
});
